<template>
<div class="page add-member">
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell phone-num">
            <div class="weui-cell__hd"><label class="weui-label">部门名称</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" type="text" placeholder="必填" maxlength="15" v-model="addGroupName" :value="addGroupName">
            </div>
        </div>
        <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectGroup">
            <div class="weui-cell__bd">
                <p><span class="title">上级部门</span><span>{{prevGroupName}}</span></p>
            </div>
            <div class="weui-cell__ft">
            </div>
        </a> 
    </div>
</div> 
</template>  
<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import $ from 'zepto'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache'
    import baseApi from 'gapi/base'
    import clientInfo from 'gapi/clientInfo'
    import widget from 'gapi/widget'
    import commonPage from 'clientApi/commonPage.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                teamid:'', 
                department_id:'',  // 部门id
                addGroupName:'',  // 当前编写的小组名称
                parent_id:'',     // 上级小组id
                prevGroupName:''  // 上级小组名称
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.teamid = util.getUrddataString('teamId')
                this.department_id = util.getUrddataString('department_id')

                this.prevGroupData()
            },
            // 上级部门选择
            selectGroup(){
                let str = `teamid=${this.teamid}`
                let encodeStr = encodeURIComponent(str)
                location.href = `dmsaas://TeamDepartmentSelect?urdData=${encodeStr}`
            },
            // 获取上级部门信息
            prevGroupData(){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/team/department_info',
                    type:'GET',
                    data:{
                        team_id:this.teamid, 
                        department_id:this.department_id
                    },
                    timeout:10000,
                    success:(obj) => {
                        // console.log(obj)
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj && obj.errno == 0){
                            this.prevGroupName = obj.data.name
                            this.parent_id = obj.data.parent_id
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            submit(){
                if(!this.addGroupName){
                    widget.toast('请填写部门名称')
                    return false
                }
                commonPage.loadPageStatus('loading')
                 util.ajax({
                    url:'/team/department/create',
                    type:'POST',
                    data:{
                        team_id:this.teamid,
                        name:this.addGroupName,
                        parent_id:this.department_id
                    },
                    timeout:10000,
                    success:(obj) => {
                        // console.log(obj)
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj&&obj.errno == 0){
                            let str = '添加成功'
                            widget.toast(str)
                            commonPage.goBack(1)
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            // 更新小组
            updateData(data){
                console.log(data)
                let key = data.key
                let value = data.value
                switch(key){
                    case "selectGroup":
                        this.department_id = value.id
                        this.prevGroupName = value.name
                    break
                } 
            }
           
        },
        created(){
            
            this.init();
            //控制header
            widget.updateTitleBar({
                leftDisplay: false,
                rightDisplay: true,
                rightText: '完成',
                rightAction: 'doSubmit'
            })
            window.doSubmit = this.submit
            window.loadData = this.updateData

            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('selectDepartmentWebViewID',{'webViewID':data})
            })
        },
    }


</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src='../../static/css/common.css'></style>
<style src='../../static/css/user-list.css'></style>
